% This file computes the Cragg-Donald statistic for all six cases,
% following Stock & Yogo (2001)
%Tobias Pfutze December 2009

% Upload Data in order: pripart oppmaxpart remit_prophh_intl
% dist_border cycleB1 cycleB2 zona_norte labinc_perhh_log
% labinc_perhh_stdev illiterate indig margmuni00 poptot_2000 unempl
% raildistjuarez_05 histmig24_edoprop00 oppwin_since80 pripart1 oppmaxpart1

RAW=load('/Users/tpfutze/Dropbox/Research/Participation/Matlab/Data/participation_adjall.txt');
[N,M]=size(RAW);

% Replace total population by its logged value
RAW(:,13)=log(RAW(:,13));

% Create two separate matrices conditional on whether municipality has 
% had an opposition party in office since 1980
s0=1;
s1=1;
for i=1:N
    if (RAW(i,17)==0)
        RAW0(s0,1:M)=RAW(i,1:M);
        s0=s0+1;
    elseif RAW(i,17)==1
        RAW1(s1,1:M)=RAW(i,1:M);
        s1=s1+1;
    end
end
[N0,M0]=size(RAW0);
[N1,M1]=size(RAW1);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Create all matrices of dep., indep. and instr. variables %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Create matrices and vectors 

%Endogenous variable
y=RAW(:,3);
y0=RAW0(:,3);       
y1=RAW1(:,3);

%Included exogenous variables
x1=[ones(N,1) RAW(:,4:7)];                               
x2=[ones(N,1) RAW(:,4:14)];  

x01=[ones(N0,1) RAW0(:,4:7)];                               
x02=[ones(N0,1) RAW0(:,4:14)];  

x11=[ones(N1,1) RAW1(:,4:7)];                               
x12=[ones(N1,1) RAW1(:,4:14)];


%Set of all instruments
z1=[ones(N,1) RAW(:,15:16) RAW(:,4:7)];                               
z2=[ones(N,1) RAW(:,15:16) RAW(:,4:14)]; 

z01=[ones(N0,1) RAW0(:,15:16) RAW0(:,4:7)];                               
z02=[ones(N0,1) RAW0(:,15:16) RAW0(:,4:14)];  

z11=[ones(N1,1) RAW1(:,15:16) RAW1(:,4:7)];                               
z12=[ones(N1,1) RAW1(:,15:16) RAW1(:,4:14)];  


%Only excluded instruments
zz=RAW(:,15:16);
zz0=RAW0(:,15:16);
zz1=RAW1(:,15:16);


%%%%%%%%%%%%%%%%%%%
% DEFINE MATRICES %
%%%%%%%%%%%%%%%%%%%

GT=zeros(1,6);
CD=zeros(1,6);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%% ENTIRE SAMPLE %%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%1ST CASE
casenum=1;
Y=y;
X=x1;
Z=z1;
ZZ=zz;
[n,k1] = size(X);
[n,k2] = size(ZZ);

[gt, cd] = Cragg_Donald(Y, X, Z, ZZ, n, k1, k2);

GT(1,casenum)=gt;
CD(1,casenum)=cd;

clear Y X Z ZZ n k1 k2 gt cd;


%2ND CASE
casenum=casenum+1;
Y=y;
X=x2;
Z=z2;
ZZ=zz;
[n,k1] = size(X);
[n,k2] = size(ZZ);

[gt, cd] = Cragg_Donald(Y, X, Z, ZZ, n, k1, k2);

GT(1,casenum)=gt;
CD(1,casenum)=cd;

clear Y X Z ZZ n k1 k2 gt cd;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%   ALWAYS PRI   %%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%1ST CASE
casenum=casenum+1;
Y=y0;
X=x01;
Z=z01;
ZZ=zz0;
[n,k1] = size(X);
[n,k2] = size(ZZ);

[gt, cd] = Cragg_Donald(Y, X, Z, ZZ, n, k1, k2);
GT(1,casenum)=gt;
CD(1,casenum)=cd;

clear Y X Z ZZ n k1 k2 gt cd;


%2ND CASE
casenum=casenum+1;
Y=y0;
X=x02;
Z=z02;
ZZ=zz0;
[n,k1] = size(X);
[n,k2] = size(ZZ);

[gt, cd] = Cragg_Donald(Y, X, Z, ZZ, n, k1, k2);

GT(1,casenum)=gt;
CD(1,casenum)=cd;

clear Y X Z ZZ n k1 k2 gt cd;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%% ALREADY OPPOSITION %%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%1ST CASE
casenum=casenum+1;
Y=y1;
X=x11;
Z=z11;
ZZ=zz1;
[n,k1] = size(X);
[n,k2] = size(ZZ);

[gt, cd] = Cragg_Donald(Y, X, Z, ZZ, n, k1, k2);
GT(1,casenum)=gt;
CD(1,casenum)=cd;

clear Y X Z ZZ n k1 k2 gt cd;


%2ND CASE
casenum=casenum+1;
Y=y1;
X=x12;
Z=z12;
ZZ=zz1;
[n,k1] = size(X);
[n,k2] = size(ZZ);

[gt, cd] = Cragg_Donald(Y, X, Z, ZZ, n, k1, k2);

GT(1,casenum)=gt;
CD(1,casenum)=cd;

clear Y X Z ZZ n k1 k2 gt cd;


save PartiesCraggDonald_Results GT CD;




